/**********************************************************************************
**
** Copyright (C) 2009 Jostein Bratlie
** Contact: jostein.bratlie@gmail.com | http://bratlie.info | http://episteme.hin.no
**
** This file is part of GMQtExt, a GMlib and Qt extension library
**
** GMQtExt is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** GMQtExt is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with GMQtExt.  If not, see <http://www.gnu.org/licenses/>.
**
**********************************************************************************/


// Qt
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QGroupBox>
#include <QtGui/QSpinBox>

// GMlib
#include <gmParametrics>

// header
#include "replotwidget.h"


namespace GMQtExt {

  ReplotWidget::ReplotWidget( QWidget *parent ) : QWidget( parent ) {

    _obj = 0x0;

    QVBoxLayout *layout = new QVBoxLayout;
    setLayout( layout );

    QHBoxLayout *gb_sam_l = new QHBoxLayout;
    QGroupBox *gb_sam = new QGroupBox( "Samples" );
    gb_sam->setLayout( gb_sam_l );
    layout->addWidget( gb_sam );

    QHBoxLayout *gb_der_l = new QHBoxLayout;
    QGroupBox *gb_der = new QGroupBox( "Derivatives" );
    gb_der->setLayout( gb_der_l );
    layout->addWidget( gb_der );

    _sam_u = new QSpinBox();
    _sam_u->setMaximum( 10000 );
    _sam_u->setMinimum( 2 );
    _sam_u->setValue( 50 );
    gb_sam_l->addWidget( _sam_u );

    _sam_v = new QSpinBox();
    _sam_v->setMaximum( 10000 );
    _sam_v->setMinimum( 2 );
    _sam_v->setValue( 50 );
    _sam_v->hide();
    gb_sam_l->addWidget( _sam_v );

    _der_u = new QSpinBox();
    _der_u->setMaximum( 4 );
    _der_u->setMinimum(0 );
    _der_u->setValue( 2 );
    gb_der_l->addWidget( _der_u );

    _der_v = new QSpinBox();
    _der_v->setMaximum( 4 );
    _der_v->setMinimum(0 );
    _der_v->setValue( 2 );
    _der_v->hide();
    gb_der_l->addWidget( _der_v );

    QDialogButtonBox *bb = new QDialogButtonBox;
    bb->addButton( tr("Reset"), QDialogButtonBox::RejectRole );
    bb->addButton( tr("Plot"), QDialogButtonBox::AcceptRole );
    layout->addWidget( bb );

    connect( bb, SIGNAL(accepted()), this, SLOT(_slotReplot()) );
    connect( bb, SIGNAL(rejected()), this, SLOT(_slotReset()) );

  }

  void ReplotWidget::_slotReplot() {

    if( !_obj )
      return;

    GMlib::PCurve<float> *pcurve_float = dynamic_cast<GMlib::PCurve<float>*>( _obj );
    GMlib::PSurf<float> *psurf_float = dynamic_cast<GMlib::PSurf<float>*>( _obj );
    if( pcurve_float ) {

      pcurve_float->replot( _sam_u->value(), _der_u->value() );
    }
    else if( psurf_float ) {

      psurf_float->replot( _sam_u->value(), _sam_v->value(), _der_u->value(), _der_v->value() );
    }

    emit signReploted();
  }

  void ReplotWidget::_slotReset() {

    set( _obj );
  }

  void ReplotWidget::set( GMlib::SceneObject *obj ) {

    _sam_v->hide();
    _der_v->hide();

    _obj = obj;

    if( !obj ) {
      hide();
      return;
    }

    GMlib::PCurve<float> *pcurve_float = dynamic_cast<GMlib::PCurve<float>*>( obj );
    GMlib::PSurf<float> *psurf_float = dynamic_cast<GMlib::PSurf<float>*>( obj );
    if( pcurve_float || psurf_float )
      show();
    else
      hide();

    if( pcurve_float ) {

      _sam_u->setValue( pcurve_float->getSamples() );
      _der_u->setValue( pcurve_float->getDerivatives() );
    }
    else if( psurf_float ) {

      _sam_u->setValue( psurf_float->getSamplesU() );
      _der_u->setValue( psurf_float->getDerivativesU() );

      _sam_v->setValue( psurf_float->getSamplesV() );
      _der_v->setValue( psurf_float->getDerivativesV() );

      _sam_v->show();
      _der_v->show();

    }
  }

} // END namespace GMQtExt
